package apigateway.project.core.filter.flow;

import apigateway.project.commen.enums.ResponseCode;
import apigateway.project.commen.exception.LimitedException;
import apigateway.project.core.context.GlobalContext;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;

public class FlowControlbyServiceRule implements GatewayFLowControlRule{
    /**
     * 这里的resource可以是uri也可以是服务名
     */
    private String resource;

    public FlowControlbyServiceRule(String resource) {
        this.resource = resource;
    }

    @Override
    public void doFLowControlFilter() {
        Entry entry = null;
        try{
            entry = SphU.entry(resource);
            //如果有需要，可以进行一些其他的处理
        }catch (BlockException e){
            throw new LimitedException(ResponseCode.FLOW_CONTROL_ERROR);
        }finally {
            if(entry != null){
                entry.exit();
            }
        }
    }
}
